package cn.lrwjz.geotest;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.GridView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;

import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;

import android.view.View;

import androidx.appcompat.widget.SwitchCompat;
import cn.lrwjz.geotest.adapter.GridAdapter;
import cn.lrwjz.geotest.ui.CommunicationSetupActivity;
import cn.lrwjz.geotest.ui.ProjectMangerActivity;
import geo.com.geosdk.GeoManage;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    // 项目页面的菜单项
    private String[] projectMenuItems = {
        "项目管理", "数据文件管理", "坐标系统", 
        "计算转换参数", "基站平移校准", "线面库",
        "坐标点库", "数据文件导出", "编码集",
        "软件设置", "关于软件", "更多"
    };
    
    // 仪器页面的菜单项
    private String[] deviceMenuItems = {
        "通讯设置", "移动站模式", "基准站模式",
        "静态模式", "一键固定", "工作状态",
        "配置集", "仪器信息", "倾斜校准",
        "仪器设置", "默认电台设置", "重新定位",
        "仪器注册", "更多"
    };
    
    // 测量页面的菜单项
    private String[] measureMenuItems = {
        "点测量", "CAD", "地图校正",
        "点放样", "直线放样", "边坡放样",
        "AR测量", "线路施工放样", "线路逐点放样",
        "测横段面", "场地高程控制", "曲线放样",
        "既有线放样", "图层设置", "测区设置",
        "铁路放样", "更多"
    };
    
    // 工具页面的菜单项
    private String[] toolsMenuItems = {
        "坐标转换", "角度变换", "周长面积计算",
        "几何计算", "计算器", "大电台设置",
        "测后校正", "地面归算", "FTP共享数据",
        "分享文件", "缓曲计算", "更多"
    };
    
    // 图标数组
    private int[] projectMenuIcons = {
        R.drawable.ic_project_manage,
        R.drawable.ic_file_manage,
        R.drawable.ic_coordinate,
        R.drawable.ic_transform,
        R.drawable.ic_calibration,
        R.drawable.ic_line_surface,
        R.drawable.ic_point_lib,
        R.drawable.ic_export,
        R.drawable.ic_encode,
        R.drawable.ic_settings,
        R.drawable.ic_about,
        R.drawable.ic_more
    };

    private int[] deviceMenuIcons = {
        R.drawable.ic_communication_setup,
        R.drawable.ic_mobile_station_mode,
        R.drawable.ic_reference_station_mode,
        R.drawable.ic_static_mode,
        R.drawable.ic_one_key_fix,
        R.drawable.ic_working_status,
        R.drawable.ic_configuration_set,
        R.drawable.ic_instrument_info,
        R.drawable.ic_check_accuracy,
        R.drawable.ic_instrument_settings,
        R.drawable.ic_default_radio_settings,
        R.drawable.ic_relocate,
        R.drawable.ic_device_registration,
        R.drawable.ic_more
    };

    private int[] measureMenuIcons = {
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_more
    };

    private int[] toolsMenuIcons = {
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_menu_gallery,
        R.drawable.ic_more
    };

    
    private int[] defaultMenuIcons;  // 用于存储其他页面的通用图标数组
    private GridView gridView;
    private GridAdapter adapter;
    private int currentPage = 0; // 0:项目 1:仪器 2:测量 3:工具

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 设置Toolbar
        androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // 设置抽屉导航
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        navigationView.setNavigationItemSelectedListener(item -> {
            int id = item.getItemId();
            if (id == R.id.nav_base_change || id == R.id.nav_track) {
                // 对于带开关的菜单项,不关闭抽屉
                return false;
            }
            // 关闭左侧菜单
            // drawer.closeDrawer(GravityCompat.START);
            return true;
        });
        // 设置开关状态监听
        View baseChangeItem = navigationView.getMenu().findItem(R.id.nav_base_change).getActionView();
        View trackItem = navigationView.getMenu().findItem(R.id.nav_track).getActionView();

        if (baseChangeItem instanceof SwitchCompat) {
            ((SwitchCompat) baseChangeItem).setOnCheckedChangeListener((buttonView, isChecked) -> {
                // 处理基站坐标变化提醒开关状态变化
            });
        }

        if (trackItem instanceof SwitchCompat) {
            ((SwitchCompat) trackItem).setOnCheckedChangeListener((buttonView, isChecked) -> {
                // 处理轨迹记录开关状态变化
            });
        }

        // 初始化GridView
        gridView = findViewById(R.id.grid_view);
        adapter = new GridAdapter(this, projectMenuItems, projectMenuIcons);
        gridView.setAdapter(adapter);

        // 设置底部导航
        BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnItemSelectedListener(item -> {
            int id = item.getItemId();
            if (id == R.id.navigation_project) {
                currentPage = 0;
                updateGridView(projectMenuItems, projectMenuIcons);
                return true;
            } else if (id == R.id.navigation_device) {
                currentPage = 1;
                updateGridView(deviceMenuItems, deviceMenuIcons);
                return true;
            } else if (id == R.id.navigation_measure) {
                currentPage = 2;
                updateGridView(measureMenuItems, measureMenuIcons);
                return true;
            } else if (id == R.id.navigation_tools) {
                currentPage = 3;
                updateGridView(toolsMenuItems, toolsMenuIcons);
                return true;
            }
            return false;
        });

        // 设置GridView的点击事件
        gridView.setOnItemClickListener((parent, view, position, id) -> {
            switch (currentPage) {
                case 0: // 项目页面
                    handleProjectItemClick(position);
                    break;
                case 1: // 仪器页面
                    handleDeviceItemClick(position);
                    break;
                case 2: // 测量页面
                    handleMeasureItemClick(position);
                    break;
                case 3: // 工具页面
                    handleToolsItemClick(position);
                    break;
            }
        });
    }

    // 更新GridView的方法
    private void updateGridView(String[] items, int[] icons) {
        adapter = new GridAdapter(this, items, icons);
        gridView.setAdapter(adapter);
    }

    // 处理项目页面的点击事件
    private void handleProjectItemClick(int position) {
        String itemName = projectMenuItems[position];
        Toast.makeText(this, "项目页面: " + itemName, Toast.LENGTH_SHORT).show();
        switch (position) {
            case 0: // 项目管理
                Intent intent = new Intent(this,ProjectMangerActivity.class);
                startActivity(intent);
                break;
            case 1: // 数据文件管理
                // TODO: 跳转到数据文件管理页面
                break;
            // ... 处理其他项目页面的点击事件
        }
    }

    // 处理仪器页面的点击事件
    private void handleDeviceItemClick(int position) {
        if(position != 0 && !GeoManage.getInstance().isConnectedBlueTooth()){
            Toast.makeText(this,"通讯未连接，请先进行通讯连接!", Toast.LENGTH_SHORT).show();
            return;
        }
        switch (position) {
            case 0: // 通讯设置
                Intent intent = new Intent(this, CommunicationSetupActivity.class);
                startActivity(intent);
                break;
            case 1: // 移动站模式
                // TODO: 跳转到移动站模式页面
                break;
            // ... 处理其他仪器页面的点击事件
        }
    }

    // 处理测量页面的点击事件
    private void handleMeasureItemClick(int position) {
        String itemName = measureMenuItems[position];
        Toast.makeText(this, "测量页面: " + itemName, Toast.LENGTH_SHORT).show();
        switch (position) {
            case 0: // 点测量
                // TODO: 跳转到点测量页面
                break;
            case 1: // 线测量
                // TODO: 跳转到线测量页面
                break;
            // ... 处理其他测量页面的点击事件
        }
    }

    // 处理工具页面的点击事件
    private void handleToolsItemClick(int position) {
        String itemName = toolsMenuItems[position];
        Toast.makeText(this, "工具页面: " + itemName, Toast.LENGTH_SHORT).show();
        switch (position) {
            case 0: // 坐标反算
                // TODO: 跳转到坐标反算页面
                break;
            case 1: // 坐标正算
                // TODO: 跳转到坐标正算页面
                break;
            // ... 处理其他工具页面的点击事件
        }
    }
}